<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高级电子表格编辑器 Demo</title>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f9f9f9;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        header {
            margin-bottom: 20px;
        }

        h1 {
            font-size: 24px;
            margin-bottom: 10px;
            color: #2c3e50;
        }

        .description {
            color: #7f8c8d;
            margin-bottom: 20px;
        }

        .toolbar {
            display: flex;
            gap: 10px;
            margin-bottom: 15px;
            flex-wrap: wrap;
            align-items: center;
        }
        a {
            text-decoration: none;
            color: #fff;
        }

        .btn-box {
            margin-left: auto;
        }

        .btn-box button {
            margin-right: 12px;
        }

        button {
            padding: 8px 16px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.2s;
        }

        button:hover {
            background-color: #2980b9;
        }

        button.export {
            background-color: #2ecc71;
        }

        button.export:hover {
            background-color: #27ae60;
        }

        button.language {
            background-color: #9b59b6;
        }

        button.language:hover {
            background-color: #8e44ad;
        }

        .spreadsheet-container {
            height: 600px;
            border: 1px solid #ddd;
            background-color: white;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            border-radius: 4px;
            overflow: hidden;
        }

        footer {
            margin-top: 20px;
            text-align: center;
            font-size: 14px;
            color: #95a5a6;
        }

        .features {
            margin-top: 30px;
        }

        .features h2 {
            font-size: 18px;
            margin-bottom: 10px;
            color: #2c3e50;
        }

        .features ul {
            list-style-type: none;
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 10px;
        }

        .features li {
            padding: 10px;
            background-color: #ecf0f1;
            border-radius: 4px;
        }

        .features li::before {
            content: "✓";
            color: #27ae60;
            margin-right: 8px;
            font-weight: bold;
        }

        .event-log {
            margin-top: 20px;
            padding: 10px;
            background-color: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 4px;
            max-height: 200px;
            overflow-y: auto;
        }

        .event-log h3 {
            margin-bottom: 10px;
            font-size: 16px;
            color: #2c3e50;
        }

        .event-log-content {
            font-family: monospace;
            font-size: 13px;
            white-space: pre-wrap;
            word-break: break-all;
        }

        .event-log-item {
            margin-bottom: 5px;
            padding: 5px;
            border-bottom: 1px solid #e9ecef;
        }

        .event-log-item:last-child {
            border-bottom: none;
        }

        .event-log-time {
            color: #6c757d;
            font-size: 12px;
        }

        .event-log-type {
            font-weight: bold;
            margin-right: 5px;
        }

        .event-log-type.change {
            color: #28a745;
        }

        .event-log-type.error {
            color: #dc3545;
        }

        .event-log-type.ready {
            color: #007bff;
        }

        .event-log-type.export {
            color: #fd7e14;
        }

        .event-log-type.import {
            color: #6f42c1;
        }

        .event-log-type.info {
            color: #17a2b8;
        }

        @media (max-width: 768px) {
            .container {
                padding: 10px;
            }

            .features ul {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <header>
        <h1>高级电子表格编辑器</h1>
        <p class="description">基于MIT许可证的开源库实现，支持拖拽行和列、合并单元格、样式设置、Excel导入导出</p>
    </header>

    <div class="toolbar">
        <button id="import-btn">导入Excel</button>
        <button id="lang-zh-btn" class="language">中文</button>
        <button id="lang-en-btn" class="language">English</button>
        <button id="add-data-btn">添加示例数据</button>
        <button id="clear-log-btn">清除日志</button>
        <div class="btn-box">
            优质案例：
            <button class="export"><a href="https://flowmix.turntip.cn/">flowmix/docx文档编辑器</a></button>
            <button><a href="https://mindlink.turntip.cn/">灵语AI文档</a></button>
        </div>

    </div>

    <div id="spreadsheet" class="spreadsheet-container"></div>

    <div class="event-log">
        <h3>事件日志</h3>
        <div id="event-log-content" class="event-log-content"></div>
    </div>

    <div class="features">
        <h2>功能特点</h2>
        <ul>
            <li>拖拽行和列</li>
            <li>合并/拆分单元格</li>
            <li>单元格样式设置</li>
            <li>Excel导入导出</li>
            <li>自定义右键菜单</li>
            <li>高性能处理大数据</li>
            <li>多语言支持</li>
            <li>事件监听机制</li>
            <li>保留Excel样式</li>
            <li>MIT许可证开源库</li>
        </ul>
    </div>

    <footer>
        <p>基于 x-spreadsheet 和 SheetJS 开源库 | MIT 许可证</p>
    </footer>
</div>

<!-- 引入电子表格编辑器脚本 -->
<script src="scripts.js"></script>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const eventLog = document.getElementById('event-log-content');

        // 添加日志函数
        function addLog(type, message) {
            const now = new Date();
            const timeStr = now.toLocaleTimeString();
            const logItem = document.createElement('div');
            logItem.className = 'event-log-item';

            let content = '';
            if (typeof message === 'object') {
                try {
                    content = JSON.stringify(message, null, 2);
                    if (content.length > 200) {
                        content = content.substring(0, 200) + '... (内容已截断)';
                    }
                } catch (e) {
                    content = '无法序列化对象';
                }
            } else {
                content = message;
            }

            logItem.innerHTML = `
          <span class="event-log-time">[${timeStr}]</span>
          <span class="event-log-type ${type}">${type}</span>
          <span>${content}</span>
        `;

            eventLog.appendChild(logItem);
            eventLog.scrollTop = eventLog.scrollHeight;

            // 限制日志数量
            while (eventLog.children.length > 50) {
                eventLog.removeChild(eventLog.firstChild);
            }
        }

        // 初始化电子表格编辑器
        const editor = new SpreadsheetEditor('#spreadsheet', {
            showToolbar: true,
            showGrid: true,
            showContextmenu: true
        });

        // 注册事件监听
        editor.on('ready', () => {
            addLog('ready', '电子表格编辑器已准备就绪');
        });

        editor.on('change', (data) => {
            addLog('change', '数据已更改');
        });

        editor.on('error', (error) => {
            addLog('error', `错误: ${error.type} - ${error.error?.message || '未知错误'}`);
        });

        editor.on('export', (data) => {
            addLog('export', `已导出到: ${data.filename}`);
        });

        editor.on('import', (data) => {
            addLog('import', `已导入: ${data.file}`);
        });

        // 绑定按钮事件
        document.getElementById('import-btn').addEventListener('click', function() {
            editor.importExcel();
        });

        // document.getElementById('export-btn').addEventListener('click', function() {
        //     editor.exportExcel('电子表格.xlsx');
        // });

        // 语言切换
        document.getElementById('lang-zh-btn').addEventListener('click', function() {
            editor.setLanguage('zh-CN');
            addLog('info', '已切换到中文');
        });

        document.getElementById('lang-en-btn').addEventListener('click', function() {
            editor.setLanguage('en-US');
            addLog('info', 'Switched to English');
        });

        // 清除日志
        document.getElementById('clear-log-btn').addEventListener('click', function() {
            eventLog.innerHTML = '';
            addLog('info', '日志已清除');
        });

        // 添加示例数据
        document.getElementById('add-data-btn').addEventListener('click', function() {
            loadSampleData();
            addLog('info', '已加载示例数据');
        });

        // 示例数据
        function loadSampleData() {
            const sampleData = {
                name: 'Sheet1',
                freeze: 'A1',
                styles: [
                    {
                        bgcolor: '#f4f5f8',
                        textwrap: true,
                        color: '#900b09',
                        bold: true,
                        border: {
                            top: ['thin', '#0366d6'],
                            bottom: ['thin', '#0366d6'],
                            right: ['thin', '#0366d6'],
                            left: ['thin', '#0366d6'],
                        },
                    },
                    {
                        bgcolor: '#ffffff',
                        textwrap: false,
                        color: '#0366d6',
                        bold: false,
                    }
                ],
                merges: [],
                rows: {
                    0: {
                        cells: {
                            0: { text: '产品', style: 0 },
                            1: { text: '价格', style: 0 },
                            2: { text: '库存', style: 0 },
                            3: { text: '销量', style: 0 },
                            4: { text: '评分', style: 0 }
                        }
                    },
                    1: {
                        cells: {
                            0: { text: '笔记本电脑', style: 1 },
                            1: { text: '5999', style: 1 },
                            2: { text: '120', style: 1 },
                            3: { text: '30', style: 1 },
                            4: { text: '4.8', style: 1 }
                        }
                    },
                    2: {
                        cells: {
                            0: { text: '智能手机', style: 1 },
                            1: { text: '3999', style: 1 },
                            2: { text: '200', style: 1 },
                            3: { text: '150', style: 1 },
                            4: { text: '4.6', style: 1 }
                        }
                    },
                    3: {
                        cells: {
                            0: { text: '平板电脑', style: 1 },
                            1: { text: '2999', style: 1 },
                            2: { text: '75', style: 1 },
                            3: { text: '20', style: 1 },
                            4: { text: '4.5', style: 1 }
                        }
                    },
                    4: {
                        cells: {
                            0: { text: '智能手表', style: 1 },
                            1: { text: '1599', style: 1 },
                            2: { text: '60', style: 1 },
                            3: { text: '40', style: 1 },
                            4: { text: '4.3', style: 1 }
                        }
                    }
                },
                cols: {
                    len: 26,
                    0: { width: 120 },
                    1: { width: 100 },
                    2: { width: 100 },
                    3: { width: 100 },
                    4: { width: 100 }
                },
                validations: [],
                autofilter: {}
            };

            editor.setData(sampleData);
        }

        // 等待电子表格完全初始化后设置数据
        setTimeout(() => {
            loadSampleData();
            addLog('info', '已加载示例数据');
        }, 500);
    });
</script>
</body>
</html>